﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfPanelCanvas
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Ellipse ellipse;
        private Rectangle rectangle;
        private Point startPoint;
        private Border border;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void kan_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released 
                && !(btnBlock.IsChecked != null && btnBlock.IsChecked.Value == true))
                return;

            var pos = e.GetPosition((IInputElement)sender);
            var x = Math.Min(pos.X, startPoint.X);
            var y = Math.Min(pos.Y, startPoint.Y);

            var w = Math.Max(pos.X, startPoint.X) - x;
            var h = Math.Max(pos.Y, startPoint.Y) - y;

            // pokud je stisknuta elipsa
            if (btnEllipse.IsChecked != null && btnEllipse.IsChecked.Value == true)
            {
                ellipse.Width = w;
                ellipse.Height = h;

                Canvas.SetLeft(ellipse, x);
                Canvas.SetTop(ellipse, y);
            }
            else if (btnRectangle.IsChecked != null && btnRectangle.IsChecked.Value == true)
            {
                rectangle.Width = w;
                rectangle.Height = h;

                Canvas.SetLeft(rectangle, x);
                Canvas.SetTop(rectangle, y);
            }
            else if (btnBlock.IsChecked != null && btnBlock.IsChecked.Value == true)
            {
                var clientWidth = border.ActualWidth;
                Canvas.SetLeft(border, pos.X - clientWidth / 2.0);

                if (IsCollide(border))
                    Cursor = Cursors.No;
                else
                    Cursor = Cursors.Arrow;
            }
        }

        private void kan_MouseDown(object sender, MouseButtonEventArgs e)
        {
            startPoint = e.GetPosition((IInputElement)sender);
            if (btnEllipse.IsChecked != null && btnEllipse.IsChecked.Value == true)
            {
                ellipse = new Ellipse
                {
                    Stroke = Brushes.LightBlue,
                    Fill = Brushes.LightBlue,
                    StrokeThickness = 2
                };

                Canvas.SetTop(ellipse, startPoint.X);
                Canvas.SetLeft(ellipse, startPoint.X);
                Canvas.SetZIndex(ellipse, 1);
                kan.Children.Add(ellipse);
            }
            else if (btnRectangle.IsChecked != null && btnRectangle.IsChecked.Value == true)
            {
                rectangle = new Rectangle
                {
                    Stroke = Brushes.LightBlue,
                    Fill = Brushes.LightBlue,
                    StrokeThickness = 2
                };

                Canvas.SetTop(rectangle, startPoint.X);
                Canvas.SetLeft(rectangle, startPoint.X);
                Canvas.SetZIndex(rectangle, 1);
                kan.Children.Add(rectangle);
            }
            else if (btnBlock.IsChecked != null && btnBlock.IsChecked.Value == true)
            {
                if (!IsCollide(border))
                {
                    for (int i = 1; i < 5; i++)
                    {
                        var btn = new Button();
                        btn.Template = (ControlTemplate)FindResource("BlockWindowA");
                        Viewbox viewBox = new Viewbox();
                        viewBox.Child = btn;
                        Border b = new Border();
                        b.BorderBrush = Brushes.LightGray;
                        b.BorderThickness = new Thickness(1);
                        b.Child = viewBox;
                        b.Height = 100;

                        Canvas.SetTop(b, (i * 100));
                        Canvas.SetLeft(b, Canvas.GetLeft(border));
                        Canvas.SetZIndex(b, 2);

                        kan.Children.Add(b);
                    }

                    btnBlock_Click(this, new RoutedEventArgs());
                    isContain = false;
                    kan_MouseEnter(sender, new MouseEventArgs(e.MouseDevice, e.Timestamp));
                }
            }

        }

        private bool IsCollide(FrameworkElement ele)
        {
            double left = Canvas.GetLeft(ele);
            double right = left + ele.ActualWidth;
            double z = Canvas.GetZIndex(ele);

            foreach (FrameworkElement child in kan.Children)
            {
                if (child == ele || Canvas.GetZIndex(child) != z)
                    continue;

                double lchild = Canvas.GetLeft(child);
                double rchild = lchild + child.ActualWidth;

                // negace vse je pred ci negace vse je za
                if (!((left < lchild && right < lchild) || (left > rchild && right > rchild)))
                {
                    return true;
                }
            }

            return false;
        }


        private void kan_MouseUp(object sender, MouseButtonEventArgs e)
        {
            ellipse = null;
            rectangle = null;
        }

        private void btnBlock_Click(object sender, RoutedEventArgs e)
        {
            if (btnBlock.IsChecked != null && btnBlock.IsChecked.Value == true)
            {
                //<Button Template="{StaticResource BlockWindowA}" />
                var btn = new Button();
                btn.Template = (ControlTemplate)FindResource("BlockWindowA");
                Viewbox viewBox = new Viewbox();
                viewBox.Child = btn;
                border = new Border();
                border.BorderBrush = Brushes.LightGray;
                border.BorderThickness = new Thickness(1);
                border.Child = viewBox;
                border.Height = 100;

            }
        }

        bool isContain = false;

        private void kan_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isContain && border != null)
            {
                kan.Children.Remove(border);
                isContain = false;
            }
        }

        private void kan_MouseEnter(object sender, MouseEventArgs e)
        {
            if (!isContain && border != null)
            {
                Canvas.SetTop(border, 0);
                Canvas.SetLeft(border, 0);
                Canvas.SetZIndex(border, 2);

                kan.Children.Add(border);
                isContain = true;
            }
        }
    }
}
